/**
 * GeoSOA/util/HTMLUtils.js
 *
 * HTML utility functions
 *
 * Copyright (C) 2009 GeoSOA Research Group, Universite Laval
 * See LICENSE file under this distribution for licensing information
 * 
 * @author Etienne Dube
 * @since 20090818
 **/

dojo.provide("GeoSOA.util.HTMLUtils");


GeoSOA.util.HTMLUtils = {
		
	/**
	 * Function for converting HTML color codes to RGB objects
	 * 
	 * @param {String} htmlColorCode HTML color code in the form #RRGGBB
	 * (e.g. #00FF00 for pure green)
	 * @return {Object} RGB object with "red", "green" and "blue"
	 * integer properties 
	 */
	HTMLtoRGB: function (htmlColorCode) {
		
		var rgbColor = {
				red: 0,
				green: 0,
				blue: 0
		};
		
		if (htmlColorCode !== undefined) {
			var idx = htmlColorCode.indexOf("#") != -1; 
			if (idx != -1) {
				var hexstr = htmlColorCode.substring(idx, idx+7);
				if (hexstr.length == 6) {
					rgbColor.red = parseInt("0x" + hexstr.substring(0,2));
					rgbColor.green = parseInt("0x" + hexstr.substring(2,4));
					rgbColor.blue = parseInt("0x" + hexstr.substring(4,6));
				}
			}
		}
		
		return rgbColor;
	},
	
	
	/**
	 * Function for RGB objects to HTML color code
	 * 
	 * @param {Object} rgbObject RGB object with "red", "green" and "blue"
	 * integer properties
	 * @return {String} HTML color code in the form #RRGGBB
	 */
	RGBtoHTML: function (rgbObject) {
		
		var red = rgbObject.red || 0;
		var green = rgbObject.green || 0;
		var blue = rgbObject.blue || 0;
		
		var hexArr = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
				"A", "B", "C", "D", "E", "F"];
		var hexify = function (intColor) {
			low = Math.floor(intColor / 16);
			high = intColor - low * 16;
			return (hexArr[low] + hexArr[high]);
		};
		
		var htmlColor = "#" + hexify(red) + hexify(green) + hexify(blue);
		
		return htmlColor;
	}
};
